package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.itheima.bean.Dish;
import com.itheima.bean.SetmealDish;
import com.itheima.dao.SetmealDishDao;
import com.itheima.service.SetmealDishService;
import com.itheima.service.dishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealDishServiceImpl implements SetmealDishService {

    @Lazy
    @Resource
    private SetmealDishDao setmealDishDao;
    @Resource
    private dishService dishService;

    @Override
    public int add(SetmealDish setmealDish) {


        return setmealDishDao.insert(setmealDish);
    }

    @Override
    public List<SetmealDish> findBySetmealId(Long id) {

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();

        lqw.eq(SetmealDish::getSetmealId, id);

        return setmealDishDao.selectList(lqw).stream().map(setmealDish -> {

            Dish dish = dishService.findByDishId(setmealDish.getDishId());

            setmealDish.setImage(dish.getImage());

            return setmealDish;
        }).collect(Collectors.toList());

    }

    @Override
    public int deletBysetmealId(long setmealId) {

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();

        lqw.eq(SetmealDish::getSetmealId, setmealId);

        return setmealDishDao.delete(lqw);


    }
}
